﻿using System.Collections.Generic;
using System.Linq;
using Softelligent.Zintra.Synchronizations.ProductsAccessConsole.Entities;

namespace Softelligent.Zintra.Synchronizations.ProductsAccessConsole
{
    public class ProductMapper
    {
        private const string CategoryBasePath = "Tags";

        public ClientSDK.Products.Product Map(Product entity)
        {
            if (entity == null)
            {
                return null;
            }
            var dto = new ClientSDK.Products.Product
                            {
                                Id = entity.Id,
                                //IsArchived = entity.IsArchived,
                                //IsHidden = entity.IsHidden,
                                //IsFeatured = entity.IsFeatured,
                                //IsDeleted = entity.IsDeleted,
                                Name = entity.Name,
                                Description = entity.Description,
                                EANorUPC = entity.Reference,
                                VendorName = entity.Manufacturer,
                                //SEOName = entity.SEOName,
                                Categories = new List<string>()
                            };
            foreach (var category in entity.Categories)
            {
                dto.Categories.Add(string.Concat(CategoryBasePath, @"\", category.Name));
            }

            return dto;
        }

        public Product Map(ClientSDK.Products.Product dto)
        {
            return Map(dto, new Product());
        }

        public Product Map(ClientSDK.Products.Product dto, Product entity)
        {
            if (dto == null || entity == null)
            {
                return null;
            }

            entity.Id = dto.Id;
            //entity.IsArchived = dto.IsArchived;
            //entity.IsHidden = dto.IsHidden;
            //entity.IsDeleted = dto.IsDeleted;
            //entity.IsFeatured = dto.IsFeatured;
            entity.Name = dto.Name;
            entity.Description = dto.Description;
            entity.Reference = dto.EANorUPC;
            entity.Manufacturer = dto.VendorName;
            //entity.SEOName = dto.SEOName;
            foreach (var categoryPath in dto.Categories)
            {
                if (!string.IsNullOrEmpty(categoryPath))
                {
                    var splitted = categoryPath.Split(new[] { '\\' });
                    if (splitted.Length > 1)
                    {
                        entity.Categories.Add(new ProductCategory
                        {
                            Name = splitted[1]
                        });
                    }
                }
            }

            return entity;
        }

        public List<ClientSDK.Products.Product> Map(IEnumerable<Product> entitys)
        {
            if (entitys == null || !entitys.Any())
            {
                return new List<ClientSDK.Products.Product>();
            }

            var products = new List<ClientSDK.Products.Product>();
            foreach (var entity in entitys)
            {
                products.Add(Map(entity));
            }

            return products;
        }
    }
}
